package com.jwaoo.mobile;
import android.util.Log;

import com.dx.as.JWCache;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.log.LogUtil;
import com.dx.as.common.storage.StorageUtil;
import com.dx.as.common.sys.ScreenUtil;
import com.jwaoo.chat.ChatHelpter;
import com.jwaoo.mobile.common.crash.CrashHandler;
import com.jwaoo.mobile.common.listener.ActivityLifecycleListener;
import com.jwaoo.mobile.common.listener.CrashHandlerLisener;
import com.jwaoo.mobile.view.account.AccountBean;

/**
 * Created by jwaoo on 2017/6/15.
 */

public class ApplicationHelper {

    private static Application mApplication;
    private static AccountBean accountInfo;
    public static void init(Application application){
        mApplication = application;
        ChatHelpter.init(mApplication, JWCache.isLogin(),getIMAccount());
        initCrashHandlerLisener();
        registerUiListener();
        StorageUtil.init(mApplication,null);
        ScreenUtil.init(mApplication);
        LogUtil.init(CommonUtils.getLogDir(application),Log.ERROR);
    }

    public static void initCrashHandlerLisener(){
        CrashHandler.getInstance().init(mApplication,new CrashHandlerLisener());
    }

    public static void setAccountInfo(AccountBean bean){
        ApplicationHelper.accountInfo = bean;
        CommonUtils.saveAccount(bean.getLoginName());
        CommonUtils.saveIMAccount(bean.getId());
        CommonUtils.saveIMName(bean.getNickname());
    }
    public static int getIMAccount(){
        return CommonUtils.getIMAccount();
    }

    public static AccountBean getAccountInfo(){
        return ApplicationHelper.accountInfo;
    }

    private static void registerUiListener(){
        ActivityLifecycleListener lifecycleListener = new ActivityLifecycleListener(mApplication);
        ChatHelpter.setActivityList(lifecycleListener.getActivityList());
        mApplication.registerActivityLifecycleCallbacks(lifecycleListener);

    }
}
